<?php

// importa a biblioteca do Lumine
require_once 'Lumine/Lumine.php';
// importa as configurações do Lumine
require_once 'lumine-conf.php';
// inicia as configurações do lumine
$config = new Lumine_Configuration($lumineConfig);

// importando a classe gerada pelo lumine
Lumine::import('Editor');

class ServicoNoticia {
	public static function gravarNoticia($obj){
		$noticia = new Editor();

		if (isset($obj["id"]))
			$noticia->id = $obj["id"]; 
		
		$noticia->texto = $obj["texto"];
		$noticia->titulo = $obj["titulo"];
		
		try
		{
			$noticia->save();
			return "";
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
		
	}

	
	public static function listarNoticias($ini, $fim){
		$noticias = new Editor();

		try
		{
			$noticias->limit($ini, $fim);
			$noticias->find();				
			return $noticias->allToArray();
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
	}	

	public static function getNoticia($id){
		$noticias = new Editor();

		try
		{
			$noticias->whereAdd('id = '.$id);
			$noticias->find();				
			$reg = $noticias->allToArray();
			
			return $reg[0];
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
	}

	public static function deletarNoticia($id){
		$noticias = new Editor();

		try
		{
			$noticias->whereAdd('id = '.$id);
			$noticias->delete(true);				
			
			return "";
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
	}
	
	public static function Total(){
		$noticias = new Editor();
		
		try
		{
			$noticias->select(" count(*) as total ");
			$noticias->find();// executa a pesquisa
			$total = $noticias->allToArray();
			return $total[0]["total"];
			
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
		
	}
	
	public static function getCustomTeaser($nwords, $body) {
		$body = _filter_html($body, FILTER_HTML_ESCAPE);		
		$aWords = explode(" ", $body);
		for($i = 0; $i < $nwords; $i++) {			
			$result .= $aWords[$i]." ";
						
			if($i == count($aWords)) {
				$complete = true;		
				break;
			}
		}
		
		if(!$complete) {
			$result .= "...";	
		}
				
		return $result;
	}	
	
	
	public static function Noticias($qtd){
		$noticias = new Editor();

		try
		{
			$noticias->limit($qtd);
			$noticias->find();				
			return $noticias->allToArray();
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
	}
	
	public static function todasNoticias(){
		$noticias = new Editor();

		try
		{
			$noticias->find();				
			return $noticias->allToArray();
		}catch (Exception $e)
		{
			return $e->getMessage();
		}
	}
}


